<template>
  <view>
    <login-container
      title="快捷登录"
      show-quit
      :back="false"
      :open-type="openType"
      :button-text="buttonText"
      :disabled="disabled"
      @submit="bindLogin"
    />
    <view class="text-center">
      <text class="text-28 color-t2" @click="jump('/passport/password-login/password-login')">其他登录方式</text>
    </view>

    <log-out-dialog :show.sync="showLogOutDialog" :phone="dialogPhone" @confirm="toast('取消成功，请重新登录')" />
  </view>
</template>

<script>
import LoginContainer from '../components/login-container/login-container'
import logOutDialog from '../components/log-out-dialog/log-out-dialog'
import login from '@/common/api/login'
import { mapGetters } from 'vuex'
import { thirdPartyLogin } from '../index'
import univerifyStyle from './index'

export default {
  name: 'Login',
  components: {
    LoginContainer,
    logOutDialog
  },
  data() {
    return {
      disabled: false,
      showLogOutDialog: false,
      dialogPhone: '',
      loginCode: '',
      providerId: 'weixinCompany',
      identityTypes: 'autoCompany'
    }
  },
  computed: {
    buttonText() {
      switch (this.oauth) {
        case 'weixin':
          return '手机号快捷登录'
        default:
          return '本机号码一键登录'
      }
    },
    openType() {
      switch (this.oauth) {
        case 'app':
          return ''
        default:
          return 'getPhoneNumber'
      }
    },
    ...mapGetters({
      oauth: 'oauth'
    })
  },
  created() {
    /* #ifndef APP-PLUS*/
    this.checkCode(this.oauth)
    /* #endif*/
    /* #ifdef APP-PLUS*/
    this.preLogin()
    /* #endif*/
  },
  methods: {
    bindLogin(e) {
      /* #ifndef APP-PLUS*/
      if (e) {
        const data = {
          encryptedData: e.encryptedData,
          iv: e.iv,
          providerId: this.providerId,
          identityTypes: this.identityTypes,
          autoCreate: 1
        }
        uni.checkSession({
          success: () => {
            this.loginSuccess(data)
          },
          fail: () => {
            this.checkCode().then(() => {
              this.loginSuccess(data)
            })
          }
        })
      }
      /* #endif*/
      /* #ifdef APP-PLUS*/
      this.appLogin()
      /* #endif*/
    },
    checkCode(provider) {
      return new Promise((resolve) => {
        thirdPartyLogin(provider).then((res) => {
          this.loginCode = res.code
          resolve()
        })
      })
    },
    async loginSuccess(data) {
      data.jsCode = this.loginCode
      const res = await this.login.bindWechatPhone(data)
      if (res && res.length) {
        this.dialogPhone = res
        this.showLogOutDialog = true
      }
    },
    // app一键登录
    appLogin() {
      uni.login({
        provider: 'univerify',
        univerifyStyle,
        success: (res) => { // 登录成功
          const data = res.authResult
          this.appPhoneLogin(data)
        },
        fail: (res) => { // 登录失败
          uni.closeAuthView()
          if (res.errCode === 30002) {
            this.jump('/passport/password-login/password-login')
          } else {
            const msg = res.errMsg.split(' ')[1]
            this.toast(msg)
          }
        }
      })
    },
    // 预登录
    preLogin() {
      uni.preLogin({
        provider: 'univerify',
        fail: (res) => {
          this.disabled = true
          console.log(res)
          const msg = res.errMsg.split(' ')[1]
          this.toast(msg)
        }
      })
    },
    // app登录
    async appPhoneLogin(data) {
      await login.appPhoneLogin(data)
      uni.closeAuthView()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
